package report_log

import (
	"example/pkg/requests"
	"fmt"
	"time"
)

type RTokenBody struct {
	AccessToken  string `json:"access_token"`
	RefreshToken string `json:"refresh_token"`
	ExpiresIn    int    `json:"expires_in"`
}

func getRefreshToken(client Client, refreshToken string) (*RTokenBody, int, string, error) {

	url := "https://api.amazon.com/auth/o2/token"

	params := map[string]interface{}{
		"grant_type":    "refresh_token",
		"client_id":     client.ClientId,
		"refresh_token": refreshToken,
		"client_secret": client.ClientSecret,
	}

	var rToken RTokenBody
	resp, err := requests.Post(url, requests.WithJson(params), requests.WithTimeout(time.Second*60))
	fmt.Println(resp.StatusCode, string(resp.Body))
	fmt.Println(resp.Header())
	if err != nil {
		return nil, 200, "post method get  token error", err
	}

	_, err = resp.Json(&rToken)
	if err != nil {
		return nil, 200, "trans token json  error", err
	}

	if rToken.ExpiresIn == 0 {

	}

	return &rToken, resp.StatusCode, string(resp.Body), nil
}

var ClientMap = map[string]Client{
	"c1": {
		ClientId: "amzn1.application-oa2-client.084663234c2143c3a3bf91fe34bbdf1e",
		ClientSecret: "29f982cfd2571585c52db5ba462f302716c87fd52507dcc355582b8821a80abc",
	},
	"c2": {
		ClientId: "amzn1.application-oa2-client.be4e30f5a0b14f488677728ec04c12e0",
		ClientSecret: "c8c0f06cd18861b9cd5b4cd228bf475ca08e7f6f7af4dad384e0772296a3dd24",
	},
}

type Client struct {
	ClientId     string `json:"clientId"`
	ClientSecret string `json:"clientSecret"`
}
